package com.atguigu.xuexiplus.service.impl;

import com.atguigu.xuexiplus.entity.ChatMessageEntity;
import com.atguigu.xuexiplus.mapper.ChatMessageMapper;
import com.atguigu.xuexiplus.service.ChatMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ChatMessageServiceImpl implements ChatMessageService {

    @Autowired
    private ChatMessageMapper chatMessageMapper;

    @Override
    public void saveMessage(ChatMessageEntity msg) {
        if (msg.getCreatedAt() == null) msg.setCreatedAt(LocalDateTime.now());
        if (msg.getIsRead() == null) msg.setIsRead(0);
        chatMessageMapper.insert(msg);
    }

    @Override
    public List<ChatMessageEntity> listHistory(Long fromUserId, Long toUserId, int page, int size) {
        int offset = Math.max(0, (page - 1)) * Math.max(1, size);
        return chatMessageMapper.listHistory(fromUserId, toUserId, offset, size);
    }
}


